package com.nondo.dean.auth.service.impl;

import com.nondo.dean.auth.domain.Position;
import com.nondo.dean.auth.domain.PositionExample;
import com.nondo.dean.auth.mapper.PositionMapper;
import com.nondo.dean.auth.service.PositionService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * project：dean-cloud
 *
 * @author linfeng @ nondo
 * @date 2018/4/23
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    @Override
    public Position findByPositionId(Long id) {
        return positionMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Position record) {
        if (record.getId() == null) {
            positionMapper.insertSelective(record);
        } else {
            positionMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        positionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Page<Position> findPositionPage(Query<Position> query) {
        PositionExample example = new PositionExample();
        PositionExample.Criteria criteria = example.createCriteria();
        Position item = query.getData();
        if (item != null) {
            if (item.getId() != null) {
                criteria.andIdEqualTo(item.getId());
            }
            if (item.getCompanyId() != null) {
                criteria.andCompanyIdEqualTo(item.getCompanyId());
            }
            if (StringUtils.isNotBlank(item.getName())) {
                criteria.andNameLike("'" + item.getName() + "%'");
            }
        }


        example.setOrderByClause("id asc");
        List<Position> resultList = positionMapper.selectByExampleWithRowbounds(example, new RowBounds(query.getOffset(), query.getLimit()));
        long total = positionMapper.countByExample(example);

        Page<Position> resultPage = Page.of(query);
        resultPage.setTotalElements(total);
        resultPage.setContent(resultList);

        return resultPage;
    }

    @Override
    public Page<Position> findPositionAndCompanyIdsPage(Query<Position> query, List<Long> companyIds) {
        PositionExample example = new PositionExample();
        PositionExample.Criteria criteria = example.createCriteria();
        Position item = query.getData();
        if (item != null) {
            if (item.getId() != null) {
                criteria.andIdEqualTo(item.getId());
            }
            if (item.getCompanyId() != null) {
                criteria.andCompanyIdEqualTo(item.getCompanyId());
            }
            if (StringUtils.isNotBlank(item.getName())) {
                criteria.andNameLike("'" + item.getName() + "%'");
            }

        }
        if (companyIds != null && !companyIds.isEmpty()) {
            criteria.andCompanyIdIn(companyIds);
        }

        example.setOrderByClause("id asc");
        List<Position> resultList = positionMapper.selectByExampleWithRowbounds(example, new RowBounds(query.getOffset(), query.getLimit()));
        long total = positionMapper.countByExample(example);

        Page<Position> resultPage = Page.of(query);
        resultPage.setTotalElements(total);
        resultPage.setContent(resultList);

        return resultPage;
    }

    @Override
    public void updateCompanyNameByCompanyId(String companyName, Long companyId) {

        Position position = new Position();
        position.setCompanyName(companyName);
        PositionExample example = new PositionExample();
        example.createCriteria().andCompanyIdEqualTo(companyId);

        positionMapper.updateByExample(position, example);
    }
}
